package com.fcy.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.fcy.dialog.adapter.DialogItemAdapter;

import java.util.Arrays;
import java.util.List;


/**
 * 内容为ListView的Dialog
 */
public class MyListDialogBuilder implements AdapterView.OnItemClickListener {
    public static final int THEME_DIALOG_GRAY = 0;
    public static final int THEME_DIALOG_WHITE = 1;
    private Dialog dialog;
    private TextView showTitle;
    public OnClickListener mOnClickListener;

    // Dialog高度为wrap_content
    public static final int STYLE_DIALOG_HEIGHT_WRAP = 2;
    // Dialog高度为固定高度
    public static final int STYLE_DIALOG_HEIGHT_FIXED = 3;

    private List<String> list;
    private DialogItemAdapter adapter;

    public MyListDialogBuilder(Context context, int style, String title, String data[]) {
        this(context, style, title, Arrays.asList(data));
    }

    public MyListDialogBuilder(Context context, int style, String title, List<String> data) {
        dialog = new Dialog(context, R.style.myDialog);
        View view;
        // 可以选择白色、黑色两种主题
        // TODO 为 Dialog 设置视图
        if (style == STYLE_DIALOG_HEIGHT_FIXED) { // 固定高度
            view = LayoutInflater.from(context)
                    .inflate(R.layout.common_list_dialog_height_fixed, null);
        } else {
            // 可以设置LayoutParams 高度为wrap_content?--------
            view = LayoutInflater.from(context)
                    .inflate(R.layout.common_list_dialog_height_wrap, null);
        }

        dialog.setContentView(view);

        // TODO 为 Dialog 设置标题
        showTitle = (TextView) view.findViewById(R.id.common_dialog_title);
        showTitle.setText(title);

        // TODO 获取ListView, 并填充数据
        ListView listView = (ListView) view.findViewById(R.id.listView);

        list = data;
        adapter = new DialogItemAdapter(context, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);
    }

    /**
     * 创建Dialog
     *
     * @return 根据指定条件创建的Dialog
     */
    public Dialog create() {
        return dialog;
    }

    /**
     * 用于回调处理item点击事件，调用setItemsListener
     *
     * @param parent
     * @param view
     * @param position 点击item的位置
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 关闭Dialog
        dialog.dismiss();
        mOnClickListener.onDialogItemClick(position);
    }

    /**
     * 设置item的点击回调接口
     */
    public MyListDialogBuilder setItemsListener(OnClickListener callBack) {
        mOnClickListener = callBack;
        return this;
    }

    public void updateData(String data[]) {
        updateData(Arrays.asList(data));
    }

    /**
     * 用新的数据源更新ListView
     *
     * @param data
     */
    public void updateData(List<String> data) {
        list.clear();
        list.addAll(data);
        // 更新数据
        adapter.notifyDataSetChanged();
    }

    /**
     * 添加一条数据
     *
     * @param data
     */
    public void addData(String data) {
        list.add(data);
    }

    /**
     * 回调接口，用于处理点击ListView的item
     */
    public interface OnClickListener {
        public void onDialogItemClick(int witch);
    }
}
